package com.wxb.cardmis.service.impl;

import com.wxb.cardmis.entity.CardEntity;
import com.wxb.cardmis.entity.UserEntity;
import com.wxb.cardmis.repository.CardRepository;
import com.wxb.cardmis.service.CardService;
import com.wxb.cardmis.utils.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;

/**
 * All rights Reserved, Designed By www.super-master.cn
 *
 * @ProjectName: cardmis
 * @Package: com.wxb.cardmis.service.impl
 * @ClassName: CardServiceImpl
 * <p> </p>
 * @Author: [Wei Xiao]
 * @Date: 2022/2/21 17:53
 * @Version: V1.0
 * @Copyright: 2022  com.wei All rights reserved.
 * TODO: 注意,本文件Wei Xiao所作,如果转载或使用请标明具体出处!
 * @Date: 2022年02月21日 17:53
 * @Description: []
 **/
@Service
public class CardServiceImpl implements CardService {
    @Autowired
    private CardRepository cardRepository;
    //添加或修改名片
    @Override
    public String add(CardEntity cardEntity, HttpSession session, MultipartFile file) {
        if ("noLogin".equals(isLogin(session))){
            return "noLogin";
        }else {
            UserEntity user = (UserEntity) session.getAttribute("user");
            cardEntity.setUser(user);

            String newFileName="";
            if (file != null){
                String fileName = file.getOriginalFilename();
                newFileName = MyUtil.getNewFileName(fileName);
                String realpath = "C:\\Users\\Administrator\\Desktop\\笔记\\03在线考试系统设计\\cardmis-vue\\cardmis-vue\\static";
                File targetFile = new File(realpath,newFileName);
                if (!targetFile.exists()){
                    targetFile.mkdirs();
                }
                cardEntity.setLogo("static/"+newFileName);
                try{
                    file.transferTo(targetFile);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            cardRepository.save(cardEntity);
            return "ok";
        }
    }
    //查询登录用户的名片
    @Override
    public Object cards(HttpSession session) {
        if ("noLogin".equals(isLogin(session))){
            return "noLogin";
        }else {
            Sort sort = Sort.by(Sort.Direction.DESC,"id");
            UserEntity user = (UserEntity) session.getAttribute("user");
            return cardRepository.findByUser_id(user.getId(),sort);
        }
    }
    //根据 ID 删除名片
    @Override
    public String delete(HttpSession session, Integer cid) {
        if ("noLogin".equals(isLogin(session))){
            return "noLogin";
        }else {
            cardRepository.deleteById(cid);
            return "ok";
        }
    }
    //根据ID 获得一个名片信息
    @Override
    public Object aCard(HttpSession session, Integer cid) {
        if ("noLogin".equals(isLogin(session))){
            return "noLogin";
        }else {
            return cardRepository.getById(cid);
        }
     }
     //操作名片时,检查用户是否登录
    @Override
    public String isLogin(HttpSession session) {
        Object user = session.getAttribute("user");
        if (user == null){
            return "noLogin";
        }
        return "yes";
    }
}
